﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace RapChieuPhim
{
    public partial class QuanLyRapPhim : Form
    {
        #region Thuong # Quan li lich chieu
        LichChieuBUS lcbus = new LichChieuBUS();
        LichChieuDTO lcdto = new LichChieuDTO();
        DangNhapBUS dnbus = new DangNhapBUS();

        public QuanLyRapPhim()
        {
            InitializeComponent();
        }
        private void btnDatVe_Click(object sender, EventArgs e)
        {
            DatVe frmDatVe = new DatVe();
            frmDatVe.ShowDialog();
        }

        private void btnPhim_Click(object sender, EventArgs e)
        {
            PhimChieu frm = new PhimChieu();
            frm.Show();
        }

        public void LayDanhSachLichChieu()
        {
            dtgLichChieu.DataSource = lcbus.LayDanhSachLichChieu();
        }

        public void QuanLyRapPhim_Load(object sender, EventArgs e)
        {
            LoadPhim();
            
        }

        public  void tabQLLC_Click(object sender, EventArgs e)
        {
            LayDanhSachLichChieu();
            cbxCaChieu.DataSource = lcbus.LayDanhSachMaCaChieu();
            //cbxCaChieu.ValueMember = "MaCaChieu";
            cbxCaChieu.DisplayMember = "TenCaChieu";
            cbxMaPhim.DataSource = lcbus.LayDanhSachMaPhim();
            //cbxMaPhim.ValueMember = "MaPhim";
            cbxMaPhim.DisplayMember = "TenPhim";
            cbxTinhTrang.DataSource = lcbus.LayDanhSachMaTinhTrang();
            //cbxTinhTrang.ValueMember = "MaTinhTrang";
            cbxTinhTrang.DisplayMember = "TinhTrang";

            

        }
        int loaitk = 0;

        private void rdoTenPhim_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoTenPhim.Checked == true)
                loaitk = 1;

        }

        private void rdioCaChieu_CheckedChanged(object sender, EventArgs e)
        {
            if (rdioCaChieu.Checked == true)
                loaitk = 1;
        }

       
        
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            string tuKhoa = txtKhoa.Text.ToString();
            switch (loaitk)
            {
                case 1:
                    dtgLichChieu.DataSource = lcbus.LayDanhSachTimKiemTheoPhim(tuKhoa); break;
                case 2:
                    dtgLichChieu.DataSource = lcbus.LayDanhSachTimKiemTheoCaChieu(tuKhoa); break;
                
            }
          
        }




        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                lcdto.MaPhim = cbxMaPhim.SelectedValue.ToString();
                lcdto.NgayBatDau = DateTime.Parse(dtpNgayBatDauLC.Text);
                lcdto.NgayKetThuc = DateTime.Parse(dtpNgayKetThucLC.Text);
                lcdto.NgayChieu = txtNgayChieu.Text;
                lcdto.MaCaChieu = int.Parse(cbxCaChieu.SelectedValue.ToString());
                lcdto.MaTinhTrang =int.Parse( cbxTinhTrang.SelectedValue.ToString());
                lcbus.ThemLichChieu(lcdto);
                LayDanhSachLichChieu();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi, vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }


        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                string tenPhim = dtgLichChieu.CurrentRow.Cells[1].Value.ToString();
                DataTable dt = lcbus.LayMaPhimTheoTenPhim(tenPhim);
                DataRow dr = dt.Rows[0];
                //lcdto.MaPhim = dr[0].ToString();
                cbxMaPhim.SelectedValue = dr[0].ToString();
                lcdto.MaPhim = cbxMaPhim.SelectedValue.ToString();
                //lcdto.MaPhim = cbxMaPhim.SelectedValue.ToString();
                //lcdto.MaPhim = cbxMaPhim.SelectedValue.ToString();
                lcdto.NgayBatDau = DateTime.Parse(dtpNgayBatDauLC.Text);
                lcdto.NgayKetThuc = DateTime.Parse(dtpNgayKetThucLC.Text);
                lcdto.NgayChieu = txtNgayChieu.Text;
                lcdto.MaCaChieu = int.Parse(cbxCaChieu.SelectedValue.ToString());
                lcdto.MaTinhTrang = int.Parse(cbxTinhTrang.SelectedValue.ToString());
                lcbus.CapNhatLichChieu(lcdto);
                LayDanhSachLichChieu();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi, vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dtgLichChieu_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            string tenPhim = dtgLichChieu.CurrentRow.Cells[1].Value.ToString();
            DataTable dt = lcbus.LayMaPhimTheoTenPhim(tenPhim);
            DataRow dr = dt.Rows[0];
            cbxMaPhim.SelectedValue = dr[0].ToString();

            dtpNgayBatDauLC.Value = DateTime.Parse(dtgLichChieu.CurrentRow.Cells[2].Value.ToString());
            dtpNgayKetThucLC.Value = DateTime.Parse(dtgLichChieu.CurrentRow.Cells[3].Value.ToString());
            txtNgayChieu.Text = dtgLichChieu.CurrentRow.Cells[4].Value.ToString();

            //string tenCaChieu = dtgLichChieu.CurrentRow.Cells[5].Value.ToString();
            //DataTable dt1 = lcbus.LayMaCaChieuTheoTenCaChieu(tenCaChieu);
            //DataRow dr1 = dt1.Rows[1];
            //cbxCaChieu.SelectedValue =int.Parse( dr1[0].ToString());

                
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                string tenPhim = dtgLichChieu.CurrentRow.Cells[1].Value.ToString();
                DataTable dt = lcbus.LayMaPhimTheoTenPhim(tenPhim);
                DataRow dr = dt.Rows[0];
                //lcdto.MaPhim = dr[0].ToString();
                cbxMaPhim.SelectedValue = dr[0].ToString();
                string ngayChieu = txtNgayChieu.Text;
                lcbus.XoaLichChieu(cbxMaPhim.SelectedValue.ToString(), ngayChieu);
                LayDanhSachLichChieu();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi, vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            TaiKhoanDTO tk = new TaiKhoanDTO();
            tk.TaiKhoan = txtTaiKhoan.Text;
            tk.MatKhau = txtMatKhau.Text;
            try
            {
                DataTable dt =dnbus.DangNhap(tk);
                DataRow dr = dt.Rows[0];
                tk.MaTK = int.Parse(dr[0].ToString());
                txtTaiKhoan.Clear();
                txtMatKhau.Clear();
                if (dr[1].ToString() == "QL")
                {

                    //tabCtrQLPhim2.Enabled = true;
                    btnDangXuat.Visible = true;
                    //btnDangNhap.Visible = false;
                    //txtMatKhau.Visible = false;
                    //txtTaiKhoan.Visible = false;
                    //lbTaiKhoan.Visible = false;
                    //lbMatKhau.Visible = false;
                    grDangnhap.Visible = false;
                    lbThongBaoDN.Text = "Chào mừng " + tk.TaiKhoan + " đã đăng nhập ";
                    lbThongBaoDN.Visible = true;
                    
                }
              
               
            }
            catch (Exception)
            {
                MessageBox.Show("Tài khoản chưa đúng hoặc chưa được kích hoạt, vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDangXuat_Click(object sender, EventArgs e)
        {

            //tabCtrQLPhim2.Enabled = false;
            btnDangXuat.Visible =false ;
            btnDangNhap.Visible = true; ;
            txtMatKhau.Visible = true;
            txtTaiKhoan.Visible = true;
            lbTaiKhoan.Visible = true;
            lbMatKhau.Visible = true;
            lbThongBaoDN.Visible = false;
            //tabCtrQLPhim2.SelectedTab = tabPageTrangChu;
            
        }
        #endregion


        #region HUYNH #Quan li nhan vien 
        NhanVienBUS nvBUS = new NhanVienBUS();
       
        private void btnLoadDuLieu_Click(object sender, EventArgs e)
        {
            dtgnhanvien.DataSource = nvBUS.LoadDSNhanVien();
            //Load du lieu len comboxlop
            //DataTable dt = new DataTable();
            //dt = nvBUS.LoadDSNhanVien();
            cbxManv.DataSource = nvBUS.LoadDSNhanVien();
            cbxManv.DisplayMember = "HoTen";
            cbxManv.ValueMember = "Mã NV";
            Binding();
        }
        private void Binding()
        {
            cbxManv.DataBindings.Clear();
            cbxManv.DataBindings.Add("Text", dtgnhanvien.DataSource, "Mã NV");

            txtHoTen.DataBindings.Clear();
            txtHoTen.DataBindings.Add("Text", dtgnhanvien.DataSource, "Họ tên");

            dtpNgaySinh.DataBindings.Clear();
            dtpNgaySinh.DataBindings.Add("Text", dtgnhanvien.DataSource, "Ngày sinh");

            txtCMND.DataBindings.Clear();
            txtCMND.DataBindings.Add("Text", dtgnhanvien.DataSource, "CMND");

            cbxGioiTinh.DataBindings.Clear();
            cbxGioiTinh.DataBindings.Add("Text", dtgnhanvien.DataSource, "Giới tính");

            txtDiaChi.DataBindings.Clear();
            txtDiaChi.DataBindings.Add("Text", dtgnhanvien.DataSource, "Địa chỉ");

            txtMaChucVu.DataBindings.Clear();
            txtMaChucVu.DataBindings.Add("Text", dtgnhanvien.DataSource, "Mã cv");

            txtMaTinhTrang.DataBindings.Clear();
            txtMaTinhTrang.DataBindings.Add("Text", dtgnhanvien.DataSource, "Mã tình trạng");
        }


        private void btnThemnv_Click(object sender, EventArgs e)
        {
            string manv = cbxManv.Text;
            string hoTen = txtHoTen.Text;
            DateTime ngaysinh = dtpNgaySinh.Value;
            string cmnd = txtCMND.Text;
            string gioitinh = cbxGioiTinh.Text;
            string diachi = txtDiaChi.Text;
            string machucvu = txtMaChucVu.Text;
            int matinhtrang = int.Parse(txtMaTinhTrang.Text);
            nvBUS.ThemNhanVien(manv, hoTen, ngaysinh, cmnd, gioitinh, diachi, machucvu, matinhtrang);
            DataTable dt = new DataTable();
            MessageBox.Show("Thanh cong");
            dt = LoadDSNhanVienForm();
        }

        private DataTable LoadDSNhanVienForm()
        {
            DataTable dt = new DataTable();
            dt = nvBUS.LoadDSNhanVien();
            return dt;
        }


        private void btnXoanv_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa nhân viên này ?", "Thông báo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                string manv = cbxManv.Text;
                nvBUS.XoaMotNV(manv);
                MessageBox.Show("Xóa thành công");
                DataTable dt = new DataTable();
                dt = LoadDSNhanVienForm();
            }
        }

        private void btnSuanv_Click(object sender, EventArgs e)
        {
            string manv = cbxManv.Text;
            string hoTen = txtHoTen.Text;
            DateTime ngaysinh = dtpNgaySinh.Value;
            string cmnd = txtCMND.Text;
            string gioitinh = cbxGioiTinh.Text;
            string diachi = txtDiaChi.Text;
            string machucvu = txtMaChucVu.Text;
            int matinhtrang = int.Parse(txtMaTinhTrang.Text);
            nvBUS.SuaNhanVien(manv, hoTen, ngaysinh, cmnd, gioitinh, diachi, machucvu, matinhtrang);
            DataTable dt = new DataTable();
            dt = LoadDSNhanVienForm();
            MessageBox.Show("Thanh cong");
        }


        int kieutk = 0;
        private void btnTimKiemnv_Click(object sender, EventArgs e)
        {
            string tk = txtTuKhoanv.Text;
            switch (kieutk)
            {
                case 1:
                    dtgnhanvien.DataSource = nvBUS.TimTheoTenNV(tk);
                    break;
                case 2:
                    dtgnhanvien.DataSource = nvBUS.TimTheoMaNV(tk);
                    break;
                case 3:
                    dtgnhanvien.DataSource = nvBUS.TimTheoDiaChiNV(tk);
                    break;
                default:
                    dtgnhanvien.DataSource = nvBUS.TimTheoMaChucVuNV(tk);
                    break;
            }

            DataTable dt = new DataTable();
            dt = LoadDSNhanVienForm();
        }

        private void rdbTenNhanVien_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbTenNhanVien.Checked == true)
                kieutk = 1;
        }

        private void rdbMamv_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbMamv.Checked == true)
                kieutk = 2;
        }

        private void rdbDiaChinv_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbDiaChinv.Checked == true)
                kieutk = 3;
        }

        private void rdbMaChucVunv_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbMaChucVunv.Checked == true)
                kieutk = 4;
        }

        #endregion

        #region HUYNH #Chuc vu
        ChucVuBUS cvBUS = new ChucVuBUS();
        private void btnLoadDSCV_Click(object sender, EventArgs e)
        {
            dtgdsChucVu.DataSource = cvBUS.LoadDSChucVu();
            //Load du lieu len comboxlop
            DataTable dt = new DataTable();
            dt = cvBUS.LoadDSChucVu();
            cbxMacv.DataSource = dt;
            cbxMacv.DisplayMember = "MaChucVu";
            cbxMacv.ValueMember = "TenChucVu";
            Binding1();
        }

        private void Binding1()
        {
            cbxMacv.DataBindings.Clear();
            cbxMacv.DataBindings.Add("Text", dtgdsChucVu.DataSource, "MaChucVu");

            txtTenChuVu.DataBindings.Clear();
            txtTenChuVu.DataBindings.Add("Text", dtgdsChucVu.DataSource, "TenChucVu");
        }

        //Them mot chuc vu
        private void btnThemcv_Click(object sender, EventArgs e)
        {
            string macv = cbxMacv.Text;
            string tencv = txtTenChuVu.Text;
            cvBUS.ThemChucVu(macv, tencv);
            MessageBox.Show("Thêm thành công");
            DataTable dt = new DataTable();
            dt = LoadDSChucVuForm();
        }

        //Load lai danh sach du lieu
        private DataTable LoadDSChucVuForm()
        {
            DataTable dt = new DataTable();
            dt = cvBUS.LoadDSChucVu();
            return dt;
        }

        //Xoa mot chuc vu
        private void btnXoacv_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa chức vụ này ?", "Thông báo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                string macv = cbxMacv.Text;
                cvBUS.XoaChucVu(macv);
                MessageBox.Show("Xóa thành công");
                DataTable dt = new DataTable();
                dt = LoadDSChucVuForm();
            }
        }

        private void btnSuacv_Click(object sender, EventArgs e)
        {
            string macv = cbxMacv.Text;
            string tencv = txtTenChuVu.Text;
            cvBUS.SuaChucVu(macv, tencv);
            MessageBox.Show("Sửa thành công");
            DataTable dt = new DataTable();
            dt = cvBUS.LoadDSChucVu();
        }
        #endregion  

        #region HUYNH #Suat Chieu
        SuatChieuBUS scBUS = new SuatChieuBUS();
         private void btnLoadsc_Click(object sender, EventArgs e)
        {
            dtgDSSC.DataSource = scBUS.LoadDSSC();
            cbxMaSC.DataSource = scBUS.LoadDSSC();
            cbxMaSC.DisplayMember = "TenSuatChieu";
            cbxMaSC.ValueMember = "MaSuatChieu";
        }

         private void btnThemSC_Click(object sender, EventArgs e)
         {
             string masc = cbxMaSC.SelectedValue.ToString();
             string tensc = cbxMaSC.Text;
             string maphim = cbxMaPhimSC.Text;
             string ngaychieu = txtNgayChieuSC.Text;
             int macachieu = int.Parse(cbxMaCaChieuSC.Text);
             int matinhtrang = int.Parse(cbxTinhTrangSC.Text);
             if (MessageBox.Show("Bạn có chắc muốn thêm?", "Thêm suất chiếu", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                 scBUS.ThemSuatChieu(masc, tensc, maphim, ngaychieu, macachieu, matinhtrang);
             }
             DataTable dt = new DataTable();
             dt = LoadDSSChieu();
             
         }

         
         private DataTable LoadDSSChieu()
         {
             DataTable dt = new DataTable();
             dt = scBUS.LoadDSSC();
             return dt;
         }

         private void dtgDSSC_CellClick(object sender, DataGridViewCellEventArgs e)
         {
             cbxMaSC.Text = dtgDSSC.CurrentRow.Cells["MaSC"].Value.ToString();
             cbxMaPhimSC.Text = dtgDSSC.CurrentRow.Cells["MaPhim"].Value.ToString();

             txtNgayChieuSC.Text = dtgDSSC.CurrentRow.Cells["NgayChieu"].Value.ToString();
             cbxMaCaChieuSC.Text = dtgDSSC.CurrentRow.Cells["MaCaChieu"].Value.ToString();
             cbxTinhTrangSC.Text = dtgDSSC.CurrentRow.Cells["TinhTrang"].Value.ToString();
         }
        //Nut sua
         private void btnSua_Click(object sender, EventArgs e)
         {
             string masc = cbxMaSC.SelectedValue.ToString();
             string tensc = cbxMaSC.Text;
             string maphim = cbxMaPhimSC.Text;
             string ngaychieu = txtNgayChieuSC.Text;
             int macachieu = int.Parse(cbxMaCaChieuSC.Text);
             int matinhtrang = int.Parse(cbxTinhTrangSC.Text);
             if (MessageBox.Show("Bạn có chắc muốn sửa ?", "Sửa suất chiếu", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                 scBUS.SuaSuatChieu(masc, tensc, maphim, ngaychieu, macachieu, matinhtrang);
             }
             DataTable dt = new DataTable();
             dt = LoadDSSChieu();
         }
        #endregion


        
         private void btnThongTin_Click(object sender, EventArgs e)
         {
              ThongTin frmThongTin = new ThongTin();
                frmThongTin.ShowDialog();
         }

         #region Phuong # Quan li phim
         PhimBUS PhimBus = new PhimBUS();
         BindingSource b = new BindingSource();
         string flag = "";
         private void btnGVThem_Click(object sender, EventArgs e)
         {
             PhimDTO Ph = new PhimDTO();
             Ph.Maphim = txtmaphim.Text;
             Ph.Tenphim = txttenphim.Text;
             Ph.Namsx = int.Parse(txtnsx.Text);
             Ph.Nuocsx = txtnuocsx.Text;
             Ph.Hangphim = txthangphim.Text;
             Ph.Gioithieu = txthangphim.Text;
             Ph.Trailer = txttrailer.Text;
             Ph.Poster = txtposter.Text;
             Ph.Time = int.Parse(txtthoiluong.Text);
             Ph.Maloai = txtmaloai.Text;
             Ph.Dinhdang = "2D";
             Ph.Dsdv = "ABC";
             Ph.Tinhtrang = 1;
             PhimBus.ThemPhim(Ph);
             dgvdsphim.Refresh();
             LoadPhim();
             MessageBox.Show("Đã thêm thành công", "Thông Báo");
         }
         public void LoadPhim()
         {
             DataTable a = new DataTable();
             DataTable dt = new DataTable();
             dt = PhimBus.LoadPhim();
             b.DataSource = dt;
             dgvdsphim.DataSource = b;
         }
         public void XoaTextBox()
         {
             txtmaphim.Text = "";
             txttenphim.Text = "";
             txtnsx.Text = "";
             txtnuocsx.Text = "";
             txthangphim.Text = "";
             txtgioithieu.Text = "";
             txttrailer.Text = "";
             txtposter.Text = "";
             txtthoiluong.Text = "";
             txtmaloai.Text = "";
         }
         private void btnGVXoa_Click(object sender, EventArgs e)
         {
             if (flag != "")
             {
                 PhimBus.XoaPhim(flag);
                 flag = "";
                 XoaTextBox();
                 LoadPhim();
             }
         }
         private void btnGVSua_Click(object sender, EventArgs e)
         {
             PhimDTO Ph = new PhimDTO();
             Ph.Maphim = txtmaphim.Text;
             Ph.Tenphim = txttenphim.Text;
             Ph.Namsx = int.Parse(txtnsx.Text);
             Ph.Nuocsx = txtnuocsx.Text;
             Ph.Hangphim = txthangphim.Text;
             Ph.Gioithieu = txthangphim.Text;
             Ph.Trailer = txttrailer.Text;
             Ph.Poster = txtposter.Text;
             Ph.Time = int.Parse(txtthoiluong.Text);
             Ph.Maloai = txtmaloai.Text;
             Ph.Dinhdang = "2D";
             Ph.Dsdv = "ABC";
             Ph.Tinhtrang = 1;
             PhimBus.SuaPhim(Ph); ;
             dgvdsphim.Refresh();
             LoadPhim();
         }

         private void tabPageQLPhim_Click(object sender, EventArgs e)
         {
             LoadPhim();
         }
         private void dgvdsphim_CellContentClick(object sender, DataGridViewCellEventArgs e)
         {
             flag = dgvdsphim.CurrentRow.Cells[0].Value.ToString();
             DataTable dt = new DataTable();
             dt = PhimBus.LayMaPhim(flag);
             if (dt != null)
             {
                 DataRow dr = dt.Rows[0];
                 txtmaphim.Text = dr[0].ToString();
                 txttenphim.Text = dr[1].ToString();
                 txtnsx.Text = dr[2].ToString();
                 txtnuocsx.Text = dr[3].ToString();
                 txthangphim.Text = dr[4].ToString();
                 txtgioithieu.Text = dr[5].ToString();
                 txttrailer.Text = dr[6].ToString();
                 txtposter.Text = dr[7].ToString();
                 txtthoiluong.Text = dr[8].ToString();
                 txtmaloai.Text = dr[10].ToString();
             }
         }
        #endregion

         

        

    }

        
                
    
}
